package com.samsonsoft.spring.web.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Component;

@Component("actorsDao")
public class ActorsDAO {
	
	private NamedParameterJdbcTemplate jdbc;
	
	
	public ActorsDAO(){
		System.out.println("Successully loaded actors dao");
	
	}
	@Autowired
	public void setDataSource(DataSource jdbc){
		this.jdbc = new NamedParameterJdbcTemplate(jdbc);
	}
	
	public List<Actor> getActors(){
		
		return jdbc.query("select * from actor", new RowMapper<Actor>(){

			@Override
			public Actor mapRow(ResultSet rs, int rowNum) throws SQLException {
				Actor actor = new Actor();
				
				actor.setId(rs.getInt("actor_id"));
				actor.setFirstName(rs.getString("first_name"));
				actor.setLastName(rs.getString("last_name"));
				return actor;
			}
			
		}	
				);
	}

}
